package com.hxxy.wyp.petsLover.common.rest.base.fronted;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.Orders;
import com.hxxy.wyp.petsLover.common.entities.ShopCart;
import com.hxxy.wyp.petsLover.common.entities.vo.req.ShopCartVo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.OrdersVo;
import com.hxxy.wyp.petsLover.common.services.base.fronted.ShopCartService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ShopCartFrontRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/2/18 17:56
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/shopCart")
public class ShopCartFrontRest extends RestBase {

    @Autowired
    private ShopCartService shopCartService;


    @RequestMapping("/getAll")
    @SaCheckLogin
    public Object selectUserAllShopCart() {
        List<ShopCart> shopCartList = shopCartService.getAllShopCart();
        return ajaxSuccess(shopCartList, "200", "查询成功!");
    }


    @RequestMapping("/updateNumberAndComputeMoney")
    @SaCheckLogin
    public Object updateNumberAndComputeMoney(@RequestBody ShopCartVo shopCartVo) {

        shopCartService.updateNumberAndComputeMoney(shopCartVo);
        return ajaxSuccess("200", "修改成功!");
    }

    @RequestMapping("/putShopCart")
    @SaCheckLogin
    public Object putShopCart(@RequestParam("id") String id) {

        shopCartService.putShopCart(id);
        return ajaxSuccess("200", "加入成功!");
    }

    /**
     * 生成订单信息
     */
    @RequestMapping(value = "/produceOrder", method = RequestMethod.POST)
    @SaCheckLogin
    public Object produceOrder(@RequestBody List<ShopCart> shopCartList) {
        Orders orders = shopCartService.produceOrder(shopCartList);
        return ajaxSuccess(orders, "200", "生成订单信息成功!");
    }

    /**
     * 移出购物车商品
     */
    @RequestMapping(value = "/deleteRow", method = RequestMethod.GET)
    @SaCheckLogin
    public Object deleteShoprCart(@RequestParam("id") String id) {
        boolean b = shopCartService.removeById(id);
        if (b) {
            return ajaxSuccess("200", "删除成功!");
        }
        return ajaxError("删除失败!");
    }
}
